﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Csla.Data;
using Csla.Security;

using NH=NHibernate;
namespace Sys.Core.ExtendCsla
{
    /// <summary>
    /// 扩展的业务对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class ExtBusinessBase<T> : BusinessBase<T> where T : ExtBusinessBase<T>
    {
        /// <summary>
        /// 实现所有程序的权限判断
        /// 通过读取用户的权限列表,寻找出其中的权限
        /// </summary>
        protected static void AddObjectAuthorizationRules()
        {
            List<Sys.Security.Puerview> pList = Sys.Security.TKUserPurview.FindPuerviewByClassName(typeof(T).FullName);
            foreach (Sys.Security.Puerview item in pList)
	        {
        	    switch(item.Operator)
                {
                    case Sys.Common.OBJECT.ACCESS_EDIT:
                        AuthorizationRules.DenyEdit(typeof(T), item.UserRole);
                        break;
                    case Sys.Common.OBJECT.ACCESS_CREATE:
                        AuthorizationRules.DenyCreate(typeof(T), item.UserRole);
                        break;
                    case Sys.Common.OBJECT.ACCESS_GET:
                        AuthorizationRules.DenyGet(typeof(T), item.UserRole);
                        break;
                    case Sys.Common.OBJECT.ACCESS_DELETE:
                        AuthorizationRules.DenyDelete(typeof(T), item.UserRole);
                        break;
                }
	        }
        }
        /// <summary>
        /// 数据访问
        /// </summary>
        /// <param name="criteria"></param>
        protected override void DataPortal_Fetch(object criteria)
        {
            try
            {
                // Get an NHibernate session factory

                NH.ISession session = Sys.NHibernate.NHConfig.GetSession();
                // Open an NHibernate session from the factory

                // Fetch the Business Object
                Fetch(criteria, session);
            }
            catch (Exception ex)
            {
                string x = ex.Message;
            }
        }
        /// <summary>
        /// 对象转化
        /// </summary>
        /// <param name="businessCriteria"></param>
        /// <param name="session"></param>
        protected virtual void Fetch(object businessCriteria, NH.ISession session)
        {
            try
            {
                // Get the unique identifier of this Business Object from the business criteria
                //object identifier = GetUniqueIdentifier(businessCriteria);
                Guid gd = Guid.NewGuid();
                // Load the BO into the session using the unique identifier specified
                session.Load(this, 1);
                // Perform any initialisation required
            }
            catch (Exception ex)
            {
                string xx = ex.Message;
            }

        }
    }
}
